package com.dkm.biz.gl.domain;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

import com.dkm.base.core.helper.SpringHelper;
import com.dkm.base.core.util.string.StringUtils;
import com.dkm.biz.domain.AbstractDomain;
import com.dkm.biz.gl.persistence.dao.EavAttrOptionDao;
import com.dkm.biz.gl.persistence.entity.EavAttrOptionPo;
import com.dkm.biz.gl.repository.EavAttrOptionRepository;

/**
 * 对象功能:属性选项表 领域对象实体
 * 开发公司:SKG.COM
 * 开发人员:Yuki
 * 创建时间:2014-12-13 10:08:36
 */
@Service
@Scope("prototype")
public class EavAttrOption extends AbstractDomain<String, EavAttrOptionPo>{
	 
	 private EavAttrOptionDao eavAttrOptionDao = null;
	 
	 private EavAttrOptionRepository eavAttrOptionRepository;

	protected void init(){
		eavAttrOptionDao = SpringHelper.getBean(EavAttrOptionDao.class);
		eavAttrOptionRepository = SpringHelper.getBean(EavAttrOptionRepository.class);
		setDao(eavAttrOptionDao);
	}
	
	
	@Override
	public void save() {
		EavAttrOption parent = null;
		if(StringUtils.isNotEmpty(getData().getParentId())){
			parent = eavAttrOptionRepository.load(getData().getParentId());
		}
		boolean isNew = isNew();
		if(isNew){
			//因为设置了id后，isNew()方法就失效了
			getData().setId(getIdGenerator().genSid());			
		}		
		//设置树结构相关参数：深度、路径
		if(parent==null){						
			getData().setDepth(1);
			getData().setPath(getId());			
		}else{
			getData().setDepth(parent.getData().getDepth()+1);
			getData().setPath(parent.getData().getPath() + "." + getId());
		}		
		
		if(isNew){						
			create();
		}else{			
			update();
		}	
	
	}	
	 
}
